import { Asset, Component, Sprite, SpriteFrame, _decorator, assetManager, isValid, resources } from "cc";

const { ccclass, property } = _decorator;

@ccclass('DySprite')
export class DySprite extends Sprite {

    onDestroy(): void {
        super.onDestroy();
        if (this.spriteFrame) {
            this.spriteFrame.decRef();
        }
    }

    setSprite(path: string, bundle : string): void {
        resources.load(path + "/spriteFrame", SpriteFrame,(err, asset) => {
            this.onLoadAsset(asset);
        });
    }

    onLoadAsset(value: SpriteFrame): void {
        if (isValid(this.node,true) == false) {
            return;
        }
        if (this.spriteFrame) {
            if (this.spriteFrame.uuid == value.uuid) {
                return;
            }else{
                this.spriteFrame.decRef();
                this.spriteFrame = null;
            }
        }
        this.spriteFrame = value;
        this.spriteFrame.addRef();
    }

}
